package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.dao.FootUserFormationDao;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootUserFormation;
import org.footballmanager.pandafootball.system.form.impl.FootUserFormationSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootUserFormationMapper;
import org.footballmanager.pandafootball.system.service.FootFormationService;
import org.footballmanager.pandafootball.system.service.FootUserFormationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootUserFormationServiceImpl extends ServiceImpl<FootUserFormation> implements FootUserFormationService {
    @Autowired
    private FootUserFormationDao footUserFormationDao;
    @Autowired
    private FootUserFormationMapper footUserFormationMapper;
    @Autowired
    private FootFormationService footFormationService;

    /**
     * 查询
     *
     * @param entityname                  实体类名
     * @param map                         参数
     * @param footUserFormationSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootUserFormationSearchForm footUserFormationSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footUserFormationSearchForm);
        Page<FootUserFormation> page = PageHelper.startPage(footUserFormationSearchForm.getPageIndex(), footUserFormationSearchForm.getPageSize());
        List<FootUserFormation> list = this.findall(footUserFormationSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footUserFormationSearchForm 条件
     * @param request                     请求
     */
    @Override
    public void deleteAll(FootUserFormationSearchForm footUserFormationSearchForm, HttpServletRequest request) {
        if (footUserFormationSearchForm.getSelectIds() != null) {
            String[] result = footUserFormationSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据条件查询记录列表
     *
     * @param footUserFormationSearchForm 条件
     * @return 记录列表
     */
    @Override
    public List<FootUserFormation> findall(FootUserFormationSearchForm footUserFormationSearchForm) {
        return footUserFormationMapper.findall(footUserFormationSearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootUserFormation get(Serializable id) {
        return footUserFormationMapper.findById(id);
    }

    /**
     * 添加球队默认阵型
     *
     * @param user    用户实体类
     * @param request 请求
     */
    @Override
    public void saveNewFormation(FootUser user, HttpServletRequest request) {
        FootUserFormation formation = new FootUserFormation();
        formation.setUserId(user.getId());
        formation.setStatus(0);
        formation.setFormationId(footFormationService.getDefaultFormation());
        this.save(formation, request);
    }

    /**
     * 根据用户主键删除关系记录
     *
     * @param id      用户主键
     * @param request 请求
     */
    @Override
    public void deleteByUserId(Long id, HttpServletRequest request) {
        FootUserFormation formation = this.findByUserId(id);
        if (formation != null) {
            this.delete(formation.getId(), request);
        }
    }

    /**
     * 根据用户主键获取默认球队阵型
     *
     * @param id 用户主键
     * @return 默认球队阵型
     */
    @Override
    public FootUserFormation findByUserId(Long id) {
        Example example = new Example(FootUserFormation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", id);
        criteria.andEqualTo("status", 0);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 根据用户主键获取默认球队阵型列表
     *
     * @param id 用户主键
     * @return 球队阵型列表
     */
    @Override
    public List<FootUserFormation> findByUserIdWithoutStatus(Long id) {
        Example example = new Example(FootUserFormation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", id);
        criteria.andEqualTo("delFlag", 0);
        return this.findall(example);
    }
}
